package sample.client.view.gxt;

import sample.shared.FileExtensions;
import sample.shared.result.FileInfo;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor.Path;
import com.google.gwt.resources.client.ClientBundle;
import com.google.gwt.resources.client.ImageResource;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.safehtml.shared.SafeUri;
import com.sencha.gxt.core.client.ValueProvider;
import com.sencha.gxt.data.shared.ModelKeyProvider;
import com.sencha.gxt.data.shared.PropertyAccess;

public class Document
{
  public static ImageResources resources = GWT.create(ImageResources.class);

  public static DocProps props = GWT.create(DocProps.class);

  private SafeUri icon;

  private SafeHtml label;

  private FileInfo fileInfo;

  public interface DocProps extends PropertyAccess<Document>
  {
    @Path("name")
    ModelKeyProvider<Document> key();

    ValueProvider<Document, String> name();

    ValueProvider<Document, ImageResource> icon();
  }

  interface ImageResources extends ClientBundle
  {
    @Source("word.png")
    ImageResource wordIcon();

    @Source("excel.png")
    ImageResource excelIcon();

    @Source("pdf.png")
    ImageResource pdfIcon();
    
    @Source("outlook.png")
    ImageResource emailIcon();

    @Source("other.png")
    ImageResource otherIcon();
  }

  public Document(FileInfo fileInfo) {
    this.fileInfo = fileInfo;

    String labelStr = fileInfo.getName();
    String p = fileInfo.getRelPath();
    String[] tags = p.split("[\\\\/]+");
    if (tags.length > 1) {
      String s;
      StringBuilder sb = new StringBuilder();
      for (int i = 0; i < tags.length; i++) {
        s = tags[i];
        if (s.length() > 0) {
          if (sb.length() > 0) sb.append("/");
          sb.append(s);
        }
      }
      if (sb.length() > 0) labelStr += "<br>" + sb.toString();
    }
    label = SafeHtmlUtils.fromSafeConstant(labelStr);

    String t = getType();
    if (t.equals(FileExtensions.WORD))
      icon = resources.wordIcon().getSafeUri();
    else if (t.equals(FileExtensions.EXCEL))
      icon = resources.excelIcon().getSafeUri();
    else if (t.equals(FileExtensions.PDF))
      icon = resources.pdfIcon().getSafeUri();
    else if (t.equals(FileExtensions.EMAIL))
      icon = resources.emailIcon().getSafeUri();
    else
      icon = resources.otherIcon().getSafeUri();
  }

  public String getExt() {
    String name = fileInfo.getName().toLowerCase();

    int idx = name.indexOf(".");
    try {
      return name.substring(idx + 1, idx + 4);
    }
    catch (IndexOutOfBoundsException e) {
      return null;
    }
  }

  public String getType() {
    String ext = getExt();
    return FileExtensions.getType(ext);
  }

  public String getName() {
    return fileInfo.getName();
  }

  public String getPath() {
    return fileInfo.getRelPath();
  }

  public SafeUri getIcon() {
    return icon;
  }

  public void setIcon(SafeUri icon) {
    this.icon = icon;
  }

  public SafeHtml getLabel() {
    return label;
  }

  public void setLabel(SafeHtml label) {
    this.label = label;
  }
}
